package au.edu.unsw.infs3634.gamifiedlearning;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.adapter.QuizsListAdapter;
import au.edu.unsw.infs3634.gamifiedlearning.adapter.SendAdapter;
import au.edu.unsw.infs3634.gamifiedlearning.model.HistoryMeetingListModel;
import au.edu.unsw.infs3634.gamifiedlearning.model.SendModel;

public class SendList extends AppCompatActivity {


    private SendAdapter adapter;
    private List<SendModel> personList = new ArrayList<SendModel>();
    // 会议名称
    private String meetingName = "";
    // 参会人数
    private Integer persons = 0;
    // 参会人名字
    private StringBuilder members = new StringBuilder();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_list);

        // Back button
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle("发起会议");

        init();
        RecyclerView listView = findViewById(R.id.sendList);
        adapter = new SendAdapter(personList);
        listView.setAdapter(adapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        listView.setLayoutManager(linearLayoutManager);

        Button btSend = findViewById(R.id.bt_send);
        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 输入会议名字
                showInputDialog();
            }
        });

        // 选中参加会议的人
        adapter.setOnItemListener((item) -> {
            RadioButton radioButton = item.getIndex();
            String personName = item.getName();
            radioButton.setChecked(true);
            persons++;// 参加人数加一
            members.append(personName).append(",");
        });


    }


    //Back button
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

    private void showInputDialog() {
        /*@setView 装入一个EditView
         */
        final EditText editText = new EditText(SendList.this);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(SendList.this);
        inputDialog.setTitle("请输入会议名称:").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取会议名称
                        meetingName = editText.getText().toString();
                        String s = members.toString();
                        String[] split = s.split(",");
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < split.length; i++) {
                            if (i == split.length - 1){
                                sb.append(split[i]);
                            }else{
                                sb.append(split[i]).append(",");
                            }
                        }
                        members.deleteCharAt(members.length() - 1);
                        HistoryMeetingListModel historyMeetingListModel = null;
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                            LocalDateTime now = LocalDateTime.now();
                            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                            historyMeetingListModel = new HistoryMeetingListModel(meetingName, format, sb.toString(), persons.toString());
                        }
                        // 保存历史记录
                        historyMeetingListModel.save();
                        Intent intent = new Intent(SendList.this , VideoMeetting.class);
                        startActivity(intent);

                    }
                }).show();
    }

    private void init() {
        RadioButton radioButton1 = new RadioButton(this);
        RadioButton radioButton2 = new RadioButton(this);
        RadioButton radioButton3 = new RadioButton(this);
        RadioButton radioButton4 = new RadioButton(this);
        RadioButton radioButton5 = new RadioButton(this);
        RadioButton radioButton6 = new RadioButton(this);
        RadioButton radioButton7 = new RadioButton(this);
        RadioButton radioButton8 = new RadioButton(this);
        RadioButton radioButton9 = new RadioButton(this);
        SendModel send01 = new SendModel(radioButton1, "李武");
        SendModel send02 = new SendModel(radioButton2, "李平");
        SendModel send03 = new SendModel(radioButton3, "欣嘉");
        SendModel send04 = new SendModel(radioButton4, "刘德泽");
        SendModel send05 = new SendModel(radioButton5, "邓海阳");
        SendModel send06 = new SendModel(radioButton6, "徐涵衍");
        SendModel send07 = new SendModel(radioButton7, "许鸿畴");
        SendModel send08 = new SendModel(radioButton8, "宋鸿云");
        SendModel send09 = new SendModel(radioButton9, "徐鸿祯");

        personList.add(send01);
        personList.add(send02);
        personList.add(send03);
        personList.add(send04);
        personList.add(send05);
        personList.add(send06);
        personList.add(send07);
        personList.add(send08);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
        personList.add(send09);
    }
}
